use std::collections::HashMap;

use model_graph_types::{
    database::{ConnectorProtocol, DataSource as DatabaseDataSource, DatabaseType},
    datasource::{
        AttributeConditionBuilder, DataSourceConnector, QueryCondition, UpdateCondition,
        UpdateConditionBuilder,
    },
    modeling::{
        application::ApplicationConversation, set_system_meta_database, ClassGetter,
        ComparsionOperator, Value,
    },
};

use datasource_connector::connector::SimpleDataSourceConnector;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    set_system_meta_database(DatabaseDataSource {
        id: 0,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/rrg01.sqlite"),
        ..Default::default()
    })?;

    let datasource2 = DatabaseDataSource {
        id: 1,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/app.sqlite"),
        ..Default::default()
    };
    //
    let mut values = HashMap::new();
    // values.insert(
    //     String::from("application_identifier"),
    //     Value::String(String::from("application_identifier")),
    // );
    values.insert(String::from("title"), Value::String(String::from("测试")));

    let res = SimpleDataSourceConnector::default()
        .instance_update(
            &None,
            &datasource2,
            &ApplicationConversation::get_identifier()?,
            UpdateConditionBuilder::default()
                .attributes(vec![AttributeConditionBuilder::default()
                    .name("identifier")
                    .operator(ComparsionOperator::Equal)
                    .value("e4a28793-f6d9-46ff-a924-3a7361c5e01d")
                    .build()?])
                .build()?,
            values,
            None,
        )
        .await;

    match res {
        Ok(response) => {
            tracing::debug!("{}", serde_json::to_string(&response).expect(""));
        }
        Err(err) => {
            tracing::error!("{:?}", err);
        }
    }

    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
